package chapter12.section2;
/* 程序12-1：获得IP地址信息*/

import java.net.InetAddress;
import java.net.UnknownHostException;

public class GetLocalHostTest {
    public static void main(String[] args) {
        InetAddress myIP = null; //声明IP地址对象变量
        try {
            //下面的语句显示了获得本机IP地址及主机名的方法
            myIP = InetAddress.getLocalHost(); //获得本机IP地址
            System.out.println("HostAddress is " + myIP.getHostAddress());
            System.out.println("HostName is " + myIP.getHostName());
            //下面的语句显示了获得指定主机名的IP地址的方法
            myIP = InetAddress.getByName("java.sun.com");//获得指定域名的地址
            System.out.println("HostAddress is " + myIP.getHostAddress());
            System.out.println("HostName is " + myIP.getHostName());
        } catch (UnknownHostException e) {
            //忽略异常
        }
    }
} 
